﻿using InfonavaEye.handler;
using InfonavaEye.Handler;
using InfonavaEye.model;
using Sylan.Common.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InfonavaEye.Forms
{
    public partial class CameraPreviewForm : Form
    {
        private PLCHandler plcHandler = PLCHandler.Instance;

        private CameraHandler cameraHandler = CameraHandler.Instance;

        private XYZ targetXYZ;

        public CameraPreviewForm(XYZ targetXYZ)
        {
            this.targetXYZ = targetXYZ;
            InitializeComponent();
        }

        private void CameraPreviewForm_Load(object sender, EventArgs e)
        {
            int drawingBoxWidth = pictureBox.Width;
            pictureBox.Width = (int)(pictureBox.Height / 2048f * 3072f);
            pictureBox.Location = new Point(pictureBox.Location.X + (drawingBoxWidth - pictureBox.Width) / 2, pictureBox.Location.Y);

            ThreadHelper.ExcuteThreadTask(() =>
            {
                plcHandler.StartPLC();
                cameraHandler.play(this.pictureBox);
                moveToTarget();
            },10);
        }

        private void moveToTarget() {
            ThreadHelper.ExcuteThreadTask(() =>
            {
                try
                {
                    plcHandler.moveToXYZandCapture(targetXYZ.X, targetXYZ.Y, targetXYZ.Z, false);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            },500);
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CameraPreviewForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            cameraHandler.pause();
        }
    }
}
